<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="4" :xs="24" >
        <div class="device-tree" style="height: 750px">
          <el-scrollbar style="height:100%">
            <el-tree
              :props="props"
              :load="loadNode"
              lazy
              accordion
              @node-click="handleNodeClick"
            >
            </el-tree>
          </el-scrollbar>
        </div>
      </el-col>
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="区县名称" prop="areaDesc">
            <el-input
              v-model="queryParams.areaDesc"
              placeholder="请输入区县名称"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table
          v-loading="loading"
          :data="areaList"
          row-key="areaCode"
          default-expand-all
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
        >
          <el-table-column label="区县代码" prop="areaCode"/>
          <el-table-column label="区县名称" align="center" prop="areaDesc"/>
          <el-table-column label="父级区县代码" align="center" prop="parentAreaCode"/>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['business:area:edit']"
              >修改
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-plus"
                @click="handleAdd(scope.row)"
                v-hasPermi="['business:area:add']"
              >新增</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <!-- 添加或修改区县信息维护对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="区县代码" prop="areaCode">
          <el-input v-model="form.areaCode" placeholder="请输入区县code" :disabled="disabled"/>
        </el-form-item>
        <el-form-item label="区县名称" prop="areaDesc">
          <el-input v-model="form.areaDesc" placeholder="请输入区县名称" />
        </el-form-item>
        <el-form-item label="父级区县代码" prop="parentAreaCode">
          <el-input v-model="form.parentAreaCode" placeholder="请输入父级区县code" :disabled="true"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
    import { listArea, getArea, addArea, updateArea, treeselect } from "@/api/business/area/area";
    import Treeselect from "@riophae/vue-treeselect";
    import "@riophae/vue-treeselect/dist/vue-treeselect.css";
    import { areaByParent } from '@/api/area';

    export default {
        name: "Area",
        components: {
            Treeselect
        },
        data() {
            return {
                // 遮罩层
                loading: true,
                // 显示搜索条件
                showSearch: true,
                // 区县信息维护表格数据
                areaList: [],
                // 总条数
                total: 0,
                // 区县信息维护树选项
                areaOptions: [],
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    areaType:null,
                    areaDesc: null,
                    id:null,
                    areaCode: null,
                    parentAreaCode: null
                },
                // 表单参数
                form: {},
                // 表单校验
                rules: {
                },
                defaultProps: {
                    children: "children",
                    label: "label"
                },
                // 查询参数
                query: {
                    areaType:null,
                    areaDesc: null,
                    id:null,
                    areaCode: null,
                    parentAreaCode: null
                },
                disabled:false,
                area: {
                    areaType:null,
                    areaDesc: null,
                    id:null,
                    areaCode: null,
                    parentAreaCode: null
                },
                props: {
                    label: 'name',
                    children: 'zones',
                    isLeaf: 'leaf'
                }
            };
        },
        created() {
            this.getList();
            this.getTreeselect();
        },
        methods: {
            /** 查询区县信息维护列表 */
            getList() {
                this.loading = true;
                listArea(this.queryParams).then(response => {
                    this.areaList = response.rows;
                    this.total = response.total;
                    this.loading = false;
                });
            },
            /** 转换区县信息维护数据结构 */
            normalizer(node) {
                if (node.children && !node.children.length) {
                    delete node.children;
                }
                return {
                    id: node.areaCode,
                    label: node.areaDesc,
                    children: node.children
                };
            },
            /** 查询区县信息维护下拉树结构 */
            getTreeselect() {
                this.loading = true;
                this.query.areaType = "2";
                treeselect(this.query).then(response => {
                    this.areaOptions = response.data;
                    this.loading = false;
                });
            },
            // 筛选节点
            filterNode(value, data) {
                if (!value) return true;
                return data.label.indexOf(value) !== -1;
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                    id: null,
                    areaCode: null,
                    areaType: null,
                    areaDesc: null,
                    parentAreaCode: null,
                };
                this.resetForm("form");
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.queryParams.parentAreaCode=null;
                this.resetForm("queryForm");
                this.handleQuery();
            },
            /** 新增按钮操作 */
            handleAdd(row) {
                this.reset();
                this.disabled=false;
                this.getTreeselect();
                if (row != null && row.areaCode) {
                    this.form.parentAreaCode = row.areaCode;
                } else {
                    this.form.parentAreaCode = 0;
                }
                this.form.areaType = row.areaType+1;
                console.log(this.form);
                this.open = true;
                this.title = "添加区县信息维护";
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                this.disabled=true;
                this.getTreeselect();
                if (row != null) {
                    this.form.parentAreaCode = row.areaCode;
                }
                getArea(row.id).then(response => {
                    this.form = response.data;
                    this.open = true;
                    this.title = "修改区县信息维护";
                });
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        if (this.form.id != null) {
                            updateArea(this.form).then(response => {
                                this.$modal.msgSuccess("修改成功");
                                this.open = false;
                                this.getList();
                            });
                        } else {
                            addArea(this.form).then(response => {
                                this.$modal.msgSuccess("新增成功");
                                this.open = false;
                                this.getList();
                            });
                        }
                    }
                });
            },
            loadNode(node, resolve) {
                if (node.level === 0) {
                    return resolve([{ name: '中国', id: '1' }])
                }
                areaByParent(node.data.id).then(result => {
                    const datas = [];
                    result.data.forEach(item => {
                        const data = { name: item.n, id: item.v, codept: item.pid}
                        datas.push(data);
                    })
                    resolve(datas)
                })
            },
            // 节点单击事件
            handleNodeClick(data) {
                this.queryParams.parentAreaCode = data.id;
                this.getList();
            },
        }
    };
</script>
<style>
  .el-scrollbar .el-scrollbar__wrap {overflow-x: hidden;}
  .el-tree>.el-tree-node{

    height: 500px;
    min-width: 100%;
    display:inline-block;
  }
</style>
